package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.battle.BattleType;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.camptower.CampTowerHandle;

import java.util.ArrayList;

/**
 * 阵营塔层信息
 *
 * @ClassName CMD_CampTowerFloor
 * @Author jingtao-zheng
 * @Date 2022/8/2 15:01
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerFloor, classtype = CampTowerMessage.CampTowerFloorRequest.class)
public class CMD_CampTowerFloor extends Command<CampTowerMessage.CampTowerFloorRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerFloorRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        int floor = request.getFloor();

        CampTowerMessage.CampTowerFloorResponse.Builder response = CampTowerMessage.CampTowerFloorResponse.newBuilder();

        BattleType battleType = CampTowerHandle.matchBattleType(type);

        CampTowerHandle handle = new CampTowerHandle(battleType);

        //封装最快通过和最少战力通过
        handle.packTowerFloor(playerCode, floor, response,type);

        getTotalPower(handle,floor,response);

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerFloorResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    //获取总战力
    public void getTotalPower(CampTowerHandle handle , int floor , CampTowerMessage.CampTowerFloorResponse.Builder response){
        ArrayList<FighterModule> fighterModuleList = handle.getFighterModuleList(floor);
        long totalPower = 0;
        for (FighterModule fighterModule : fighterModuleList) {
            long power = fighterModule.getTotalPower();
            totalPower += power;
        }
        response.setTotalPower(totalPower);
    }
}
